package com.passengerflowmanagementsystem.web.controller.em;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.passengerflowmanagementsystem.statistics.domain.hk.HkResult;
import com.passengerflowmanagementsystem.statistics.domain.hk.HkRootRegionResult;
import com.passengerflowmanagementsystem.statistics.domain.hk.RootRegion;
import com.passengerflowmanagementsystem.statistics.domain.vo.RegionVo;
import com.passengerflowmanagementsystem.statistics.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.passengerflowmanagementsystem.common.annotation.Log;
import com.passengerflowmanagementsystem.common.core.controller.BaseController;
import com.passengerflowmanagementsystem.common.core.domain.AjaxResult;
import com.passengerflowmanagementsystem.common.enums.BusinessType;
import com.passengerflowmanagementsystem.statistics.domain.EmExhibitionShop;
import com.passengerflowmanagementsystem.statistics.service.IEmExhibitionShopService;
import com.passengerflowmanagementsystem.common.utils.poi.ExcelUtil;
import com.passengerflowmanagementsystem.common.core.page.TableDataInfo;

/**
 * 展馆Controller
 * 
 * @author sheng
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/em/shop")
public class EmExhibitionShopController extends BaseController
{
    @Autowired
    private IEmExhibitionShopService emExhibitionShopService;

    @Autowired
    private IRegionService regionService;

    /**
     * 查询展馆
     */
    @GetMapping("/list")
    public TableDataInfo list(EmExhibitionShop emExhibitionShop)
    {
        startPage();
        List<EmExhibitionShop> list = emExhibitionShopService.selectEmExhibitionShopList(emExhibitionShop);
        return getDataTable(list);
    }

    /**
     * 导出展馆列表
     */
    @Log(title = "导出展馆列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmExhibitionShop emExhibitionShop)
    {
        List<EmExhibitionShop> list = emExhibitionShopService.selectEmExhibitionShopList(emExhibitionShop);
        ExcelUtil<EmExhibitionShop> util = new ExcelUtil<EmExhibitionShop>(EmExhibitionShop.class);
        util.exportExcel(response, list, "展馆数据");
    }

    /**
     * 获取展馆详细信息
     */
    @GetMapping(value = "/{shopId}")
    public AjaxResult getInfo(@PathVariable("shopId") Long shopId)
    {
        return success(emExhibitionShopService.selectEmExhibitionShopByShopId(shopId));
    }

    /**
     * 新增展馆
     */
    @Log(title = "新增展馆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmExhibitionShop emExhibitionShop)
    {
        String hkRootRegionStr = regionService.regionsRoot();
        HkRootRegionResult hkRootRegionResult = JSONObject.parseObject(hkRootRegionStr, HkRootRegionResult.class);
        RootRegion rootRegion = hkRootRegionResult.getData();
        //新增海康区域
        RegionVo regionVo = new RegionVo();
        regionVo.setClientId(emExhibitionShop.getShopId());
        regionVo.setParentIndexCode(rootRegion.getIndexCode());
        regionVo.setRegionName(emExhibitionShop.getShopName());
        regionVo.setRegionType("10");
        regionVo.setDescription(emExhibitionShop.getRemark());
        RegionVo[] regionVos = new RegionVo[]{regionVo};
        String str = regionService.regionBatchAdd(regionVos);
        HkResult hkResult = JSONObject.parseObject(str, HkResult.class);
        //获取海康区域id并关联到展厅
        emExhibitionShop.setHkRegionIndexCode(hkResult.getData().getSuccesses()[0].getRegionIndexCode());
        return toAjax(emExhibitionShopService.insertEmExhibitionShop(emExhibitionShop));
    }

    /**
     * 修改展馆
     */
    @Log(title = "修改展馆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmExhibitionShop emExhibitionShop)
    {
        RegionVo regionVo = new RegionVo();
        regionVo.setRegionIndexCode(emExhibitionShop.getHkRegionIndexCode());
        regionVo.setRegionName(emExhibitionShop.getShopName());
        regionService.regionSingleUpdate(regionVo);
        return toAjax(emExhibitionShopService.updateEmExhibitionShop(emExhibitionShop));
    }

    /**
     * 删除展馆
     */
    @Log(title = "删除展馆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public AjaxResult remove(@PathVariable Long[] shopIds)
    {
        ArrayList<String> array = new ArrayList<>();
        for (int i = 0; i < shopIds.length; i++) {
            EmExhibitionShop emExhibitionShop = emExhibitionShopService.selectEmExhibitionShopByShopId(shopIds[i]);
            array.add(emExhibitionShop.getHkRegionIndexCode());
        }
        String[] indexCodes = new String[array.size()];
        array.toArray(indexCodes);
        regionService.regionBatchDelete(indexCodes);
        return toAjax(emExhibitionShopService.deleteEmExhibitionShopByShopIds(shopIds));
    }
}
